﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace msBsc.WebUi.wMall.Buyer
{
    public partial class wfCommitDemand : msBsc.Cls.BPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        public override void DoAction()
        {
            if (strAction == "InitInfo")//显示初始化信息
            {
                SetInitInfo();
            }
            else if (strAction == "CommitDemand")//用户提交需求信息
            {
                CommitUserNeeds();
            }
        }

        /// <summary>
        /// 设置初始化信息
        /// </summary>
        public void SetInitInfo()
        {
            string routeOrderId = this.GetParamHtValue("routeOrderId");

            if (string.IsNullOrWhiteSpace(routeOrderId))
            {
                this.Alert("线路需求信息有误，请确认");
                return;
            }

            using (msBsc.AppLogic.DataAlc.CBizDjkOrderService objCBizDjkOrderService = new AppLogic.DataAlc.CBizDjkOrderService())
            {
                System.Data.DataSet ds = objCBizDjkOrderService.SelectById(routeOrderId);

                if (ds == null
                    || ds.Tables[0].Rows.Count <= 0)
                {
                    this.Alert("线路需求信息不存在");
                    return;
                }

                if (ds.Tables[0].Rows[0]["ORDER_ROUTE_STATUS"].ToString() != "0")
                {
                    this.Alert("该条需求信息已提交，并正在处理中，请勿重复提交");
                    return;
                }

                this.ExecJScript(@"ShowInitInfo('" + ds.Tables[0].Rows[0]["COUNTRY_NME"].ToString() + "','" + ds.Tables[0].Rows[0]["CITY_NME"].ToString() + "','" + ds.Tables[0].Rows[0]["DESTINATION_NME"].ToString() + "','" + ds.Tables[0].Rows[0]["ORDER_ROUTE_DAYS"].ToString() + "','" + ds.Tables[0].Rows[0]["ORDER_ROUTE_NME"].ToString() + "','" + ds.Tables[0].Rows[0]["VEHICLE_TYPE_NME"].ToString() + "','" + ds.Tables[0].Rows[0]["ORDER_VEHICLE_SEATS"].ToString() + "','" + ds.Tables[0].Rows[0]["VEHICLE_BRAND_NME"].ToString() + "','" + ds.Tables[0].Rows[0]["ORDER_DJACK_NME"].ToString() + "','" + ds.Tables[0].Rows[0]["TOTAL_CHARGE"].ToString() + "');");
            }
        }


        /// <summary>
        /// 用户提交需求信息
        /// </summary>
        private void CommitUserNeeds()
        {
            string serviceDt = this.GetParamHtValue("serviceDt");
            string serviceAddr = this.GetParamHtValue("serviceAddr");
            string custName = this.GetParamHtValue("custName");
            string custPhone = this.GetParamHtValue("custPhone");
            string routeOrderId = this.GetParamHtValue("routeOrderId");


            DateTime sDt;
            if (string.IsNullOrWhiteSpace(serviceDt)
                || !DateTime.TryParse(serviceDt, out sDt)
                || sDt < DateTime.UtcNow)
            {
                this.ExecJScript(@"$("".result-msg"").text(""请选择接待您的时间"").css(""color"", ""#f00"")");
                return;
            }

            if (string.IsNullOrWhiteSpace(serviceAddr))
            {
                this.ExecJScript(@"$("".result-msg"").text(""请填写接待您的详细地址"").css(""color"", ""#f00"")");
                return;
            }

            if (string.IsNullOrWhiteSpace(custName))
            {
                this.ExecJScript(@"$("".result-msg"").text(""请填写接待您的姓名，以确保我们的工作人员联系到您，为您更好的服务"").css(""color"", ""#f00"")");
                return;
            }

            if (string.IsNullOrWhiteSpace(custPhone))
            {
                this.ExecJScript(@"$("".result-msg"").text(""请填写接待您的联系电话，以确保我们的工作人员联系到您，为您更好的服务"").css(""color"", ""#f00"")");
                return;
            }

            if (string.IsNullOrWhiteSpace(routeOrderId))
            {
                this.ExecJScript(@"$("".result-msg"").text(""该条路线需求信息有误，不能进行提交"").css(""color"", ""#f00"")");
                return;
            }

            using (msBsc.AppLogic.DataAlc.CBizDjkOrderService objCBizDjkOrderService = new AppLogic.DataAlc.CBizDjkOrderService())
            {
                System.Data.DataSet ds = objCBizDjkOrderService.SelectById(routeOrderId);

                if (ds == null || ds.Tables[0].Rows.Count <= 0)
                {
                    this.ExecJScript(@"$("".result-msg"").text(""该条路线需求信息不存在，不能进行提交"").css(""color"", ""#f00"")");
                    return;
                }

                if (ds.Tables[0].Rows[0]["ORDER_ROUTE_STATUS"].ToString() != "0")
                {
                    this.ExecJScript(@"$("".result-msg"").text(""该条需求信息已提交，并正在处理中，请勿重复提交"").css(""color"", ""#f00"")");
                    return;
                }

                ds.Tables[0].Rows[0]["BEGIN_SERVICE_DT"] = sDt;
                ds.Tables[0].Rows[0]["BEGIN_SERVICE_ADDR"] = serviceAddr;
                ds.Tables[0].Rows[0]["BUYER_NME"] = custName;
                ds.Tables[0].Rows[0]["BUYER_PHONE"] = custPhone;
                ds.Tables[0].Rows[0]["ORDER_ROUTE_STATUS"] = 1;

                objCBizDjkOrderService.Update(ds);

                this.ExecJScript(@"$("".result-msg"").text(""您的需求已成功提交，我们的客服人员将在30分钟内与您联系，确认您的行程安排。"")");
            }
        }
    }
}